
package de.justphil.tcg.tcgandroidclient.network.rest;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import android.content.Context;

import de.justphil.tcg.tcgandroidclient.R;
import de.justphil.tcg.tcgandroidclient.Storage;
import de.justphil.tcg.tcgandroidclient.content.TCGContentAdapter;
import de.justphil.tcg.tcgandroidclient.network.Connectivity;
import de.justphil.tcg.tcgandroidclient.network.NoInternetException;
import de.justphil.tcg.tcgandroidclient.network.rest.Http.Method;
import de.justphil.tcg.tcgandroidclient.util.StringTools;
import de.justphil.tcg.tcgandroidclient.util.TCGLog;

public class RESTHelper {
	
	private static final String CONTENT_TYPE = "application/json";
	public static final int CODE_NOT_MODIFIED = 304;
	public static final int CODE_OK = 200;
	public static final int CODE_CREATED = 201;
	
	public static ResponseContainer restRequest(Context context, String url,
			HashMap<String, String> headers, Method method) {
	
		return restRequest(context, url, "", headers, method);
	}
	
	public static String getBodyStr(ResponseContainer rc) {
	
		try {
			return new String(rc.getBytes(), "UTF-8");
		}
		catch (Exception e) {
			throw new IllegalArgumentException("The ResponseContainer could not be processed!");
		}
	}
	
	public static ResponseContainer restRequest(Context context, String url, String body,
			HashMap<String, String> headers, Method method) {
	
//		if(method == Method.GET){
//			throw new IllegalArgumentException("Use this method only for PUT and POST requests!");
//		}
		try {
			return restRequest(context, url, body.getBytes("UTF-8"), headers, method);
		}
		catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	private static ResponseContainer restRequest(Context context, String url, byte[] data,
			HashMap<String, String> headers, Method method) {
	
		if (url == null || url == "" || method == null) {
			throw new IllegalArgumentException("Not all necessary arguments have been supplyed!");
		}
		url = StringTools.getString(new Object[] {Storage.getRestServer(), url });
		TCGLog.i(new Object[] {
				"REST request to url: ", url });
		if (headers == null) {
			headers = new HashMap<String, String>();
		}
		if (data == null) {
			data = new byte[0];
		}
		Connectivity conn = new Connectivity();
		Http http = Http.getInstance();
		if (!http.isInitialized()) {
			http.init(conn, context);
		}
		String authorization = new TCGContentAdapter(context).getAuthorization();
		TCGLog.d(new Object[] {
				"   Method: ", method });
		if (authorization != null) {
			headers.put("Authorization", authorization);
			TCGLog.d(new Object[] {
					"   => Authorization: ", authorization });
		}
		if (method == Method.POST || method == Method.PUT) {
			TCGLog.v(new Object[] {
					"   => Content-Type: ", CONTENT_TYPE });
			headers.put("Content-Type", CONTENT_TYPE);
			try {
				String bodyStr = new String(data, "UTF-8");
				TCGLog.v(new Object[] {
						"   => Body: ", bodyStr });
			}
			catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		ResponseContainer rc = null;
		try {
			rc = http.req(url, data, headers, method);
			if (rc != null) {
				TCGLog.d(new Object[] {
						"   <= ResponseCode: ", rc.getResponseCode() });
				String bodyStr = null;
				try {
					bodyStr = new String(rc.getBytes(), "UTF-8");
				}
				catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				if (!"".equals(bodyStr)) {
					TCGLog.v(new Object[] {
							"   <= Body: ", bodyStr });
				}
			}
		}
		catch (NoInternetException e) {
			TCGLog.w(new Object[] {
					"REST Trying to request URL ", url, " without internet connection!" });
		}
		catch (Error e) {
			TCGLog.e(new Object[] {
					"REST Could not reach REST Server for URL ", url, "!" });
		}
		return rc;
	}
}
